# This tool selects a folder and then collects place references for each text file in that folder.
# It outputs the data for each text file as a .csv in a specified location, using the text file name as a label for the .csv.
# Importing packages
import tkinter
import os
import shutil
from shutil import copyfile
from tkinter import filedialog
from tkinter.filedialog import askdirectory
import nltk
from nltk import word_tokenize
import csv
import re
from re import compile
# Selecting input, output folders
print("Please select the directory in which your corpus files are contained.")	
selectstart = filedialog.askdirectory()
print("Please select the folder in which you would like your output files placed.")
selectend = filedialog.askdirectory()
os.chdir(selectstart)
startloc = os.getcwd()
for filenames in os.listdir(startloc):
	path = os.path.join(startloc, filenames)
	if os.path.isdir(path):
		print("Found Sub Directory. Skipping.")
		continue
	else : 
		with open(os.path.join(startloc, filenames)) as myfile:
			print("Opening:", myfile.name)
			basename = os.path.basename(myfile.name)
			print("Running metrics for:", basename)
			basenamestr = str(basename)
			nameonly = basenamestr[:-4]
#NLTK processing, length count
			txt =open(myfile.name, encoding='utf8')
			raw = txt.read()
			tokens = word_tokenize(raw)
			nltktext = nltk.Text(tokens)
			lowernltk = [w.lower() for w in nltktext]
			totallen = len(lowernltk)
#Building state location counts
			ALtotal = (raw.count("Alabama")+raw.count("Huntsville")+raw.count("Mobile")+raw.count("Tuscaloosa"))
			AKtotal = (raw.count("Alaska")+raw.count("Anchorage")+raw.count("Juneau")+raw.count("Wasilla")+raw.count("Sitka"))
			AZtotal = (raw.count("Arizona")+raw.count("Phoenix")+raw.count("Tucson")+raw.count("Scottsdale")+raw.count("Tempe"))
			ARtotal = (raw.count("Arkansas")+raw.count("Little Rock")+raw.count("Fort Smith")+raw.count("Springdale"))
			CAtotal = (raw.count("California")+raw.count("Los Angeles")+raw.count("L.A.")+raw.count("San Diego")+raw.count("San Jose")+raw.count("San Francisco")+raw.count("Sacramento")+raw.count("Fresno")+raw.count("Long Beach")+raw.count("Oakland")+raw.count("Bakersfield")+raw.count("Anaheim")+raw.count("Chula Vista")+raw.count("Irvine")+raw.count("San Bernardino"))
			COtotal = (raw.count("Colorado")+raw.count("Denver")+raw.count("Fort Collins"))
			CTtotal = (raw.count("Connecticut")+raw.count("Bridgeport")+raw.count("New Haven")+raw.count("Stamford")+raw.count("Hartford"))
			DEtotal = (raw.count("Delaware"))
			FLtotal = (raw.count("Florida")+raw.count("Jacksonville")+raw.count("Miami")+raw.count("Tampa")+raw.count("Orlando")+raw.count("Tallahassee")+raw.count("Hialeah"))
			GAtotal = (raw.count("Georgia")+raw.count("Atlanta")+raw.count("Savannah")+raw.count("Sandy Springs"))
			HItotal = (raw.count("Hawaii")+raw.count("Honolulu")+raw.count("Mililani")+raw.count("Pearl City")+raw.count("Waipahu"))
			IDtotal = (raw.count("Idaho")+raw.count("Boise")+raw.count("Meridian")-raw.count("Prime Meridian")+raw.count("Nampa"))
			ILtotal = (raw.count("Illinois")+raw.count("Chicago")+raw.count("Windy City")+raw.count("Joliet")+raw.count("Rockford")+raw.count("Naperville"))
			INtotal = (raw.count("Indiana")+raw.count("Indianapolis")+raw.count("Fort Wayne")+raw.count("Evansville")+raw.count("South Bend"))
			IAtotal = (raw.count("Iowa")+raw.count("Des Moines")+raw.count("Cedar Rapids")+raw.count("Davenport")+raw.count("Sioux City"))
			KStotal = (raw.count("Kansas")-raw.count("Kansas City")+raw.count("Wichita")+raw.count("Overland Park")+raw.count("Olathe")+raw.count("Topeka"))
			KYtotal = (raw.count("Kentucky")+raw.count("Louisville")+raw.count("Owensboro")+raw.count("Frankfort"))
			LAtotal = (raw.count("Louisiana")+raw.count("New Orleans")+raw.count("Big Easy")+raw.count("NOLA")+raw.count("Baton Rouge")+raw.count("Shreveport"))
			MEtotal = (raw.count("Maine")+raw.count("Bangor")+raw.count("South Portland"))
			MDtotal = (raw.count("Maryland")+raw.count("Baltimore")+raw.count("Silver Spring")+raw.count("Gaithersburg")+raw.count("Rockville")+raw.count("Annapolis"))
			MAtotal = (raw.count("Massachusetts")+raw.count("Boston"))
			MItotal = (raw.count("Michigan")-raw.count("Lake Michigan")+raw.count("Detroit")+raw.count("Grand Rapids")+raw.count("Sterling Heights")+raw.count("Ann Arbor")+raw.count("Lansing"))
			MNtotal = (raw.count("Minnesota")+raw.count("Minneapolis")+raw.count("Twin Cities")+raw.count("Duluth")+raw.count("Brooklyn Park"))
			MStotal = (raw.count("Mississippi")-raw.count("the Mississippi")-raw.count("Mississippi River")+raw.count("the Mississippi River")-raw.count("the mighty Mississippi")+raw.count("Southaven")+raw.count("Hattiesburg"))
			MOtotal = (raw.count("Missouri")+raw.count("St. Louis")+raw.count("Lee's Summit")+raw.count("Jefferson City"))
			MTtotal = (raw.count("Montana")+raw.count("Billings")+raw.count("Great Falls")+raw.count("Bozeman")+raw.count("Kalispell"))
			NEtotal = (raw.count("Nebraska")+raw.count("Omaha")+raw.count("Grand Island"))
			NVtotal = (raw.count("Nevada")+raw.count("Las Vegas")+raw.count("Sin City")+raw.count("Reno")+raw.count("Sparks")+raw.count("Carson City"))
			NHtotal = (raw.count("New Hampshire")+raw.count("Nashua")+raw.count("Concord")+raw.count("Derry"))
			NJtotal = (raw.count("New Jersey")+raw.count("Jersey City")+raw.count("Woodbridge")+raw.count("Trenton")+raw.count("Jersey City"))
			NMtotal = (raw.count("New Mexico")+raw.count("Albuquerque")+raw.count("Las Cruces")+raw.count("Rio Rancho")+raw.count("Santa Fe"))
			NYtotal = (raw.count("New York")+raw.count("NYC")+raw.count("Big Apple")+raw.count("Never Sleeps")+raw.count("Manhattan")+raw.count("Brooklyn")+raw.count("Queens")+raw.count("The Bronx")+raw.count("Staten Island")+raw.count("Buffalo")+raw.count("Syracuse")+raw.count("Albany"))
			NCtotal = (raw.count("North Carolina")+raw.count("Greensboro")+raw.count("Winston-Salem"))
			NDtotal = (raw.count("North Dakota")+raw.count("Fargo")+raw.count("Grank Forks")+raw.count("Minot"))
			OHtotal = (raw.count("Ohio")+raw.count("Cincinnati")+raw.count("Akron"))
			OKtotal = (raw.count("Oklahoma")+raw.count("Tulsa")+raw.count("Broken Arrow"))
			ORtotal = (raw.count("Oregon")+raw.count("Beaverton"))
			PAtotal = (raw.count("Pennsylvania")+raw.count("Philadelphia")+raw.count("Pittsburgh")+raw.count("Allentown")+raw.count("Erie")-raw.count("Lake Erie")+raw.count("Harrisburg"))
			RItotal = (raw.count("Rhode Island")+raw.count("Providence")+raw.count("Pawtucket"))
			SCtotal = (raw.count("South Carolina")+raw.count("Rock Hill")+raw.count("Summerville"))
			SDtotal = (raw.count("South Dakota")+raw.count("Sioux Falls")+raw.count("Rapid City")+raw.count("Brookings"))
			TNtotal = (raw.count("Tennessee")+raw.count("Nashville")+raw.count("Memphis")+raw.count("Knoxville")+raw.count("Chattanooga"))
			TXtotal = (raw.count("Texas")+raw.count("San Antonio")+raw.count("Dallas")+raw.count("Fort Worth")+raw.count("Corpus Christi")+raw.count("Plano")+raw.count("Laredo")+raw.count("Laredo"))
			UTtotal = (raw.count("Utah")+raw.count("Salt Lake")+raw.count("West Valley City")+raw.count("Provo")+raw.count("West Jordan"))
			VTtotal = (raw.count("Vermont")+raw.count("Rutland City")+raw.count("Montpelier"))
			VAtotal = (raw.count("Virginia")-raw.count("West Virginia")+raw.count("Norfolk")+raw.count("Richmond")+raw.count("Newport News"))
			WAtotal = (raw.count("Washington State")+raw.count("State of Washington")+raw.count("Seattle")+raw.count("Spokane")+raw.count("Tacoma")+raw.count("Bellevue")+raw.count("Olympia"))
			WVtotal = (raw.count("West Virginia")+raw.count("Morgantown")+raw.count("Parkersberg")+raw.count("Wheeling"))
			WItotal = (raw.count("Wisconsin")+raw.count("Milwaukee")+raw.count("Green Bay")+raw.count("Kenosha")+raw.count("Racine"))
			WYtotal = (raw.count("Wyoming")+raw.count("Cheyenne")+raw.count("Laramie")+raw.count("Rock Springs"))
			DCtotal = (raw.count("D.C.")+raw.count("District of Columbia")+raw.count("Washington DC"))
#Territory counts
			AStotal = (raw.count("American Samoa")+raw.count("Pago Pago"))
			GUtotal = (raw.count("Guam")+raw.count("Hagatna"))
			MPtotal = (raw.count("Mariana Islands")+raw.count("Saipan"))
			PRtotal = (raw.count("Puerto Rico")+raw.count("San Juan"))
			VItotal = (raw.count("Virgin Islands")+raw.count("Charlotte Amalie"))
#International location counts 
			UKtotal = (raw.count("Britain")+raw.count("England")+raw.count("Scotland")+raw.count("U.K.")+raw.count("London")+raw.count("Glasgow")+raw.count("Newcastle")+raw.count("Leeds")+raw.count("Sheffield")+raw.count("Liverpool")+raw.count("Bristol")+raw.count("Wolverhampton")+raw.count("Sunderland"))
			Spaintotal = (raw.count("Spain")+raw.count("Madrid")+raw.count("Barcelona")+raw.count("Valencia")+raw.count("Seville")+raw.count("Zaragoza"))
			Greecetotal = (raw.count("Greece")+raw.count("Tessaloni")+raw.count("Patras")+raw.count("Heraklion")+raw.count("Volos")+raw.count("Rhodes")+raw.count("Crete"))
			Russiatotal =  (raw.count("Russia")+raw.count("Moscow")+raw.count("Novosibirisk")+raw.count("Yekaterinburg")+raw.count("Nizhny")+raw.count("Omsk"))
			Irelandtotal = (raw.count("Ireland")+raw.count("Dublin")+raw.count("Cork")+raw.count("Limerick")+raw.count("Laoghaire")+raw.count("Galway"))
#International city disambiguation
			#Athens
			ConTotalAthens = GAtotal+Greecetotal
			if ConTotalAthens > 0 :
				GAConPerAthens = GAtotal/ConTotalAthens
				GreeceConPerAthens = Greecetotal/ConTotalAthens
			else :
				GAConPerAthens = 0
				GreeceConPerAthens = 0
			AmbTotalAthens = raw.count("Athens")
			GAtotal = (GAtotal+(AmbTotalAthens*GAConPerAthens))
			Greecetotal = (Greecetotal+(AmbTotalAthens*GreeceConPerAthens))
			#Derry
			ConTotalDerry = DEtotal+Irelandtotal
			if ConTotalDerry > 0 :
				DEConPerDerry = DEtotal/ConTotalDerry
				IrelandConPerDerry = Irelandtotal/ConTotalDerry
			else :
				DEConPerDerry = 0
				IrelandConPerDerry = 0
			AmbTotalDerry = raw.count("Derry")
			DEtotal = (DEtotal+(AmbTotalDerry*DEConPerDerry))
			Irelandtotal = (Irelandtotal+(AmbTotalDerry*IrelandConPerDerry))
			#Toledo
			ConTotalToledo = OHtotal+Spaintotal
			if ConTotalToledo > 0 :
				OHConPerToledo = OHtotal/ConTotalToledo
				SpainConPerToledo = Spaintotal/ConTotalToledo
			else :
				OHConPerToledo = 0
				SpainConPerToledo = 0
			AmbTotalToledo = raw.count("Toledo")
			OHtotal = (OHtotal+(AmbTotalToledo*OHConPerToledo))
			Spaintotal = (Spaintotal+(AmbTotalToledo*SpainConPerToledo))
			#St. Petersburg
			ConTotalStPetersburg = FLtotal+Russiatotal
			if ConTotalStPetersburg > 0 :
				FLConPerStPetersburg = FLtotal/ConTotalStPetersburg
				RussiaConPerStPetersburg = Russiatotal/ConTotalStPetersburg
			else :
				FLConPerStPetersburg = 0
				RussiaConPerStPetersburg = 0
			AmbTotalStPetersburg = (raw.count("St. Petersburg")+raw.count("Saint Petersburg"))
			FLtotal = (FLtotal+(AmbTotalStPetersburg*FLConPerStPetersburg))
			Russiatotal = (Russiatotal+(AmbTotalStPetersburg*RussiaConPerStPetersburg))
			#Birmingham
			ConTotalBirmingham = ALtotal+UKtotal
			if ConTotalBirmingham > 0 :
				ALConPerBirmingham = ALtotal/ConTotalBirmingham
				UKConPerBirmingham = UKtotal/ConTotalBirmingham
			else :
				ALConPerBirmingham = 0
				UKConPerBirmingham = 0
			AmbTotalBirmingham = raw.count("Birmingham")
			ALtotal = (ALtotal+(AmbTotalBirmingham*ALConPerBirmingham))
			UKtotal = (UKtotal+(AmbTotalBirmingham*UKConPerBirmingham))
			#Dover
			ConTotalDover = DEtotal+UKtotal
			if ConTotalDover > 0 :
				DEConPerDover = DEtotal/ConTotalDover
				UKConPerDover = UKtotal/ConTotalDover
			else :
				DEConPerDover = 0
				UKConPerDover = 0
			AmbTotalDover = raw.count("Dover")
			DEtotal = (DEtotal+(AmbTotalDover*DEConPerDover))
			UKtotal = (UKtotal+(AmbTotalDover*UKConPerDover))
			#Worcester
			ConTotalWorcester = MAtotal+UKtotal
			if ConTotalWorcester > 0 :
				MAConPerWorcester = MAtotal/ConTotalWorcester
				UKConPerWorcester = UKtotal/ConTotalWorcester
			else :
				MAConPerWorcester = 0
				UKConPerWorcester = 0
			AmbTotalWorcester = raw.count("Worcester")
			MAtotal = (MAtotal+(AmbTotalWorcester*MAConPerWorcester))
			UKtotal = (UKtotal+(AmbTotalWorcester*UKConPerWorcester))
			#Cambridge
			ConTotalCambridge = MAtotal+UKtotal
			if ConTotalCambridge > 0 :
				MAConPerCambridge = MAtotal/ConTotalCambridge
				UKConPerCambridge = UKtotal/ConTotalCambridge
			else :
				MAConPerCambridge = 0
				UKConPerCambridge = 0
			AmbTotalCambridge = raw.count("Cambridge")
			MAtotal = (MAtotal+(AmbTotalCambridge*MAConPerCambridge))
			UKtotal = (UKtotal+(AmbTotalCambridge*UKConPerCambridge))
			#Manchester
			ConTotalManchester = NHtotal+UKtotal
			if ConTotalManchester > 0 :
				NHConPerManchester = NHtotal/ConTotalManchester
				UKConPerManchester = UKtotal/ConTotalManchester
			else :
				NHConPerManchester = 0
				UKConPerManchester = 0
			AmbTotalManchester = raw.count("Manchester")
			NHtotal = (NHtotal+(AmbTotalManchester*NHConPerManchester))
			UKtotal = (UKtotal+(AmbTotalManchester*UKConPerManchester))
			#Warwick
			ConTotalWarwick = RItotal+UKtotal
			if ConTotalWarwick > 0 :
				RIConPerWarwick = RItotal/ConTotalWarwick
				UKConPerWarwick = UKtotal/ConTotalWarwick
			else :
				RIConPerWarwick = 0
				UKConPerWarwick = 0
			AmbTotalWarwick = raw.count("Warwick")
			RItotal = (RItotal+(AmbTotalWarwick*RIConPerWarwick))
			UKtotal = (UKtotal+(AmbTotalWarwick*UKConPerWarwick))
			#Essex
			ConTotalEssex = UKtotal+VTtotal
			if ConTotalEssex > 0 :
				UKConPerEssex = UKtotal/ConTotalEssex
				VTConPerEssex = VTtotal/ConTotalEssex
			else :
				UKConPerEssex = 0
				VTConPerEssex = 0
			AmbTotalEssex = raw.count("Essex")
			UKtotal = (UKtotal+(AmbTotalEssex*UKConPerEssex))
			VTtotal = (VTtotal+(AmbTotalEssex*VTConPerEssex))
			#Kansas City
			ConTotalKansasCity = MOtotal+KStotal
			if ConTotalKansasCity > 0 :
				MOConPerKansasCity = MOtotal/ConTotalKansasCity
				KSConPerKansasCity = KStotal/ConTotalKansasCity
			else :
				MOConPerKansasCity = 0
				KSConPerKansasCity = 0
			AmbTotalKansasCity = raw.count("Kansas City")
			MOtotal = (MOtotal+(AmbTotalKansasCity*MOConPerKansasCity))
			KStotal = (KStotal+(AmbTotalKansasCity*KSConPerKansasCity))
#US city disambiguation
			#Fayetteville
			ConTotalFayetteville = AKtotal+NCtotal
			if ConTotalFayetteville > 0 :
				AKConPerFayetteville = AKtotal/ConTotalFayetteville
				NCConPerFayetteville = NCtotal/ConTotalFayetteville
			else :
				AKConPerFayetteville = 0
				NCConPerFayetteville = 0
			AmbTotalFayetteville = raw.count("Fayetteville")
			AKtotal = (AKtotal+(AmbTotalFayetteville*AKConPerFayetteville))
			NCtotal = (NCtotal+(AmbTotalFayetteville*NCConPerFayetteville))
			#Aurora
			ConTotalAurora = COtotal+ILtotal
			if ConTotalAurora > 0 :
				COConPerAurora = COtotal/ConTotalAurora
				ILConPerAurora = ILtotal/ConTotalAurora
			else :
				COConPerAurora = 0
				ILConPerAurora = 0
			AmbTotalAurora = raw.count("Aurora")
			COtotal = (COtotal+(AmbTotalAurora*COConPerAurora))
			ILtotal = (ILtotal+(AmbTotalAurora*ILConPerAurora))
			#Wilmington
			ConTotalWilmington = DEtotal+NCtotal
			if ConTotalWilmington > 0 :
				DEConPerWilmington = DEtotal/ConTotalWilmington
				NCConPerWilmington = NCtotal/ConTotalWilmington
			else :
				DEConPerWilmington = 0
				NCConPerWilmington = 0
			AmbTotalWilmington = raw.count("Wilmington")
			DEtotal = (DEtotal+(AmbTotalWilmington*DEConPerWilmington))
			NCtotal = (NCtotal+(AmbTotalWilmington*NCConPerWilmington))
			#Newark
			ConTotalNewark = DEtotal+NJtotal
			if ConTotalNewark > 0 :
				DEConPerNewark = DEtotal/ConTotalNewark
				NJConPerNewark = NJtotal/ConTotalNewark
			else :
				DEConPerNewark = 0
				NJConPerNewark = 0
			AmbTotalNewark = raw.count("Newark")
			DEtotal = (DEtotal+(AmbTotalNewark*DEConPerNewark))
			NJtotal = (NJtotal+(AmbTotalNewark*NJConPerNewark))
			#Lexington
			ConTotalLexington = KYtotal+MAtotal
			if ConTotalLexington > 0 :
				KYConPerLexington = KYtotal/ConTotalLexington
				MAConPerLexington = MAtotal/ConTotalLexington
			else :
				KYConPerLexington = 0
				MAConPerLexington = 0
			AmbTotalLexington = raw.count("Lexington")
			KYtotal = (KYtotal+(AmbTotalLexington*KYConPerLexington))
			MAtotal = (MAtotal+(AmbTotalLexington*MAConPerLexington))
			#Bowling Green
			ConTotalBowlingGreen = KYtotal+OHtotal
			if ConTotalBowlingGreen > 0 :
				KYConPerBowlingGreen = KYtotal/ConTotalBowlingGreen
				OHConPerBowlingGreen = OHtotal/ConTotalBowlingGreen
			else :
				KYConPerBowlingGreen = 0
				OHConPerBowlingGreen = 0
			AmbTotalBowlingGreen = raw.count("Bowling Green")
			KYtotal = (KYtotal+(AmbTotalBowlingGreen*KYConPerBowlingGreen))
			OHtotal = (OHtotal+(AmbTotalBowlingGreen*OHConPerBowlingGreen))
			#Portland
			ConTotalPortland = MEtotal+ORtotal
			if ConTotalPortland > 0 :
				MEConPerPortland = MEtotal/ConTotalPortland
				ORConPerPortland = ORtotal/ConTotalPortland
			else :
				MEConPerPortland = 0
				ORConPerPortland = 0
			AmbTotalPortland = raw.count("Portland")
			MEtotal = (MEtotal+(AmbTotalPortland*MEConPerPortland))
			ORtotal = (ORtotal+(AmbTotalPortland*ORConPerPortland))
			#Lewiston
			ConTotalLewiston = MEtotal+IDtotal
			if ConTotalLewiston > 0 :
				MEConPerLewiston = MEtotal/ConTotalLewiston
				IDConPerLewiston = IDtotal/ConTotalLewiston
			else :
				MEConPerLewiston = 0
				IDConPerLewiston = 0
			AmbTotalLewiston = raw.count("Lewiston")
			MEtotal = (MEtotal+(AmbTotalLewiston*MEConPerLewiston))
			IDtotal = (IDtotal+(AmbTotalLewiston*IDConPerLewiston))
			#Bloomington
			ConTotalBloomington = MNtotal+IDtotal
			if ConTotalBloomington > 0 :
				MNConPerBloomington = MNtotal/ConTotalBloomington
				IDConPerBloomington = IDtotal/ConTotalBloomington
			else :
				MNConPerBloomington = 0
				IDConPerBloomington = 0
			AmbTotalBloomington = raw.count("Bloomington")
			MNtotal = (MNtotal+(AmbTotalBloomington*MNConPerBloomington))
			IDtotal = (IDtotal+(AmbTotalBloomington*IDConPerBloomington))
			#Gulfport
			ConTotalGulfport = MStotal+LAtotal
			if ConTotalGulfport > 0 :
				MSConPerGulfport = MStotal/ConTotalGulfport
				LAConPerGulfport = LAtotal/ConTotalGulfport
			else :
				MSConPerGulfport = 0
				LAConPerGulfport = 0
			AmbTotalGulfport = raw.count("Gulfport")
			MStotal = (MStotal+(AmbTotalGulfport*MSConPerGulfport))
			LAtotal = (LAtotal+(AmbTotalGulfport*LAConPerGulfport))
			#Rochester
			ConTotalRochester = NYtotal+MNtotal
			if ConTotalRochester > 0 :
				NYConPerRochester = NYtotal/ConTotalRochester
				MNConPerRochester = MNtotal/ConTotalRochester
			else :
				NYConPerRochester = 0
				MNConPerRochester = 0
			AmbTotalRochester = raw.count("Rochester")
			NYtotal = (NYtotal+(AmbTotalRochester*NYConPerRochester))
			MNtotal = (MNtotal+(AmbTotalRochester*MNConPerRochester))
			#Salem
			ConTotalSalem = MAtotal+ORtotal
			if ConTotalSalem > 0 :
				MAConPerSalem = MAtotal/ConTotalSalem
				ORConPerSalem = ORtotal/ConTotalSalem
			else :
				MAConPerSalem = 0
				ORConPerSalem = 0
			AmbTotalSalem = raw.count("Salem")
			MAtotal = (MAtotal+(AmbTotalSalem*MAConPerSalem))
			ORtotal = (ORtotal+(AmbTotalSalem*ORConPerSalem))
			#Charleston
			ConTotalCharleston = SCtotal+WVtotal
			if ConTotalCharleston > 0 :
				SCConPerCharleston = SCtotal/ConTotalCharleston
				WVConPerCharleston = WVtotal/ConTotalCharleston
			else :
				SCConPerCharleston = 0
				WVConPerCharleston = 0
			AmbTotalCharleston = raw.count("Charleston")
			SCtotal = (SCtotal+(AmbTotalCharleston*SCConPerCharleston))
			WVtotal = (WVtotal+(AmbTotalCharleston*WVConPerCharleston))
			#Aberdeen
			ConTotalAberdeen = SDtotal+WAtotal
			if ConTotalAberdeen > 0 :
				SDConPerAberdeen = SDtotal/ConTotalAberdeen
				WAConPerAberdeen = WAtotal/ConTotalAberdeen
			else :
				SDConPerAberdeen = 0
				WAConPerAberdeen = 0
			AmbTotalAberdeen = raw.count("Aberdeen")
			SDtotal = (SDtotal+(AmbTotalAberdeen*SDConPerAberdeen))
			WAtotal = (WAtotal+(AmbTotalAberdeen*WAConPerAberdeen))
			#Burlington
			ConTotalBurlington = IAtotal+VTtotal
			if ConTotalBurlington > 0 :
				IAConPerBurlington = IAtotal/ConTotalBurlington
				VTConPerBurlington = VTtotal/ConTotalBurlington
			else :
				IAConPerBurlington = 0
				VTConPerBurlington = 0
			AmbTotalBurlington = raw.count("Burlington")
			IAtotal = (IAtotal+(AmbTotalBurlington*IAConPerBurlington))
			VTtotal = (VTtotal+(AmbTotalBurlington*VTConPerBurlington))
#Presidential names disambiguation
			#Jackson
			TitleJacksonTotal = (raw.count("Mr. Jackson")+raw.count("Ms. Jackson")+raw.count("Mrs. Jackson")+raw.count("Dr. Jackson")+raw.count("President Jackson")+raw.count("Pres. Jackson")+raw.count("Lt. Jackson")+raw.count("Lieutenant Jackson")+raw.count("Sgt. Jackson")+raw.count("Sergent Jackson")+raw.count("Cpt. Jackson")+raw.count("Captain Jackson")+raw.count("Cpl. Jackson")+raw.count("Corporal Jackson")+raw.count("Pvt. Jackson")+raw.count("Private Jackson")+raw.count("Gen. Jackson")+raw.count("General Jackson")+raw.count("Admiral Jackson"))
			JacksonFNRE = re.compile("|".join(w + r"\s+" + "Jackson" \
										for w in nltk.corpus.names.words()))
			JacksonFNFindall = JacksonFNRE.findall(raw)
			JacksonFNTotal = len(JacksonFNFindall)
			JacksonNameTotal = (JacksonFNTotal+TitleJacksonTotal)
			ConTotalJackson = MStotal+JacksonNameTotal
			if ConTotalJackson > 0 :
				MSConPerJackson = MStotal/ConTotalJackson
				NameConPerJackson = JacksonNameTotal/ConTotalJackson
			else :
				MSConPerJackson = 0
				NameConPerJackson = 0
			RawJacksonTotal = raw.count("Jackson")
			RawJacksonLessName = (RawJacksonTotal-JacksonNameTotal)
			PerJacksonTotal = RawJacksonLessName*MSConPerJackson
			MStotal = (MStotal+PerJacksonTotal)
			#Lincoln
			TitleLincolnTotal = (raw.count("Mr. Lincoln")+raw.count("Ms. Lincoln")+raw.count("Mrs. Lincoln")+raw.count("Dr. Lincoln")+raw.count("President Lincoln")+raw.count("Pres. Lincoln")+raw.count("Lt. Lincoln")+raw.count("Lieutenant Lincoln")+raw.count("Sgt. Lincoln")+raw.count("Sergent Lincoln")+raw.count("Cpt. Lincoln")+raw.count("Captain Lincoln")+raw.count("Cpl. Lincoln")+raw.count("Corporal Lincoln")+raw.count("Pvt. Lincoln")+raw.count("Private Lincoln")+raw.count("Gen. Lincoln")+raw.count("General Lincoln")+raw.count("Admiral Lincoln"))
			LincolnFNRE = re.compile("|".join(w + r"\s+" + "Lincoln" \
										for w in nltk.corpus.names.words()))
			LincolnFNFindall = LincolnFNRE.findall(raw)
			LincolnFNTotal = len(LincolnFNFindall)
			LincolnNameTotal = (LincolnFNTotal+TitleLincolnTotal)
			ConTotalLincoln = ILtotal+LincolnNameTotal
			if ConTotalLincoln > 0 :
				ILConPerLincoln = ILtotal/ConTotalLincoln
				NameConPerLincoln = LincolnNameTotal/ConTotalLincoln
			else :
				ILConPerLincoln = 0
				NameConPerLincoln = 0
			RawLincolnTotal = raw.count("Lincoln")
			RawLincolnLessName = (RawLincolnTotal-LincolnNameTotal)
			PerLincolnTotal = RawLincolnLessName*ILConPerLincoln
			ILtotal = (ILtotal+PerLincolnTotal)
			#Cleveland
			TitleClevelandTotal = (raw.count("Mr. Cleveland")+raw.count("Ms. Cleveland")+raw.count("Mrs. Cleveland")+raw.count("Dr. Cleveland")+raw.count("President Cleveland")+raw.count("Pres. Cleveland")+raw.count("Lt. Cleveland")+raw.count("Lieutenant Cleveland")+raw.count("Sgt. Cleveland")+raw.count("Sergent Cleveland")+raw.count("Cpt. Cleveland")+raw.count("Captain Cleveland")+raw.count("Cpl. Cleveland")+raw.count("Corporal Cleveland")+raw.count("Pvt. Cleveland")+raw.count("Private Cleveland")+raw.count("Gen. Cleveland")+raw.count("General Cleveland")+raw.count("Admiral Cleveland"))
			ClevelandFNRE = re.compile("|".join(w + r"\s+" + "Cleveland" \
										for w in nltk.corpus.names.words()))
			ClevelandFNFindall = ClevelandFNRE.findall(raw)
			ClevelandFNTotal = len(ClevelandFNFindall)
			ClevelandNameTotal = (ClevelandFNTotal+TitleClevelandTotal)
			ConTotalCleveland = OHtotal+ClevelandNameTotal
			if ConTotalCleveland > 0 :
				OHConPerCleveland = OHtotal/ConTotalCleveland
				NameConPerCleveland = ClevelandNameTotal/ConTotalCleveland
			else :
				OHConPerCleveland = 0
				NameConPerCleveland = 0
			RawClevelandTotal = raw.count("Cleveland")
			RawClevelandLessName = (RawClevelandTotal-ClevelandNameTotal)
			PerClevelandTotal = RawClevelandLessName*OHConPerCleveland
			OHtotal = (OHtotal+PerClevelandTotal)
#Non-Presidential name disambiguation
			#Fairbanks
			TitleFairbanksTotal = (raw.count("Mr. Fairbanks")+raw.count("Ms. Fairbanks")+raw.count("Mrs. Fairbanks")+raw.count("Dr. Fairbanks")+raw.count("President Fairbanks")+raw.count("Pres. Fairbanks")+raw.count("Lt. Fairbanks")+raw.count("Lieutenant Fairbanks")+raw.count("Sgt. Fairbanks")+raw.count("Sergent Fairbanks")+raw.count("Cpt. Fairbanks")+raw.count("Captain Fairbanks")+raw.count("Cpl. Fairbanks")+raw.count("Corporal Fairbanks")+raw.count("Pvt. Fairbanks")+raw.count("Private Fairbanks")+raw.count("Gen. Fairbanks")+raw.count("General Fairbanks")+raw.count("Admiral Fairbanks"))
			FairbanksFNRE = re.compile("|".join(w + r"\s+" + "Fairbanks" \
										for w in nltk.corpus.names.words()))
			FairbanksFNFindall = FairbanksFNRE.findall(raw)
			FairbanksFNTotal = len(FairbanksFNFindall)
			FairbanksNameTotal = (FairbanksFNTotal+TitleFairbanksTotal)
			ConTotalFairbanks = AKtotal+FairbanksNameTotal
			if ConTotalFairbanks > 0 :
				AKConPerFairbanks = AKtotal/ConTotalFairbanks
				NameConPerFairbanks = FairbanksNameTotal/ConTotalFairbanks
			else :
				AKConPerFairbanks = 0
				NameConPerFairbanks = 0
			RawFairbanksTotal = raw.count("Fairbanks")
			RawFairbanksLessName = (RawFairbanksTotal-FairbanksNameTotal)
			PerFairbanksTotal = RawFairbanksLessName*AKConPerFairbanks
			AKtotal = (AKtotal+PerFairbanksTotal)
			#Lafayette
			TitleLafayetteTotal = (raw.count("Mr. Lafayette")+raw.count("Ms. Lafayette")+raw.count("Mrs. Lafayette")+raw.count("Dr. Lafayette")+raw.count("President Lafayette")+raw.count("Pres. Lafayette")+raw.count("Lt. Lafayette")+raw.count("Lieutenant Lafayette")+raw.count("Sgt. Lafayette")+raw.count("Sergent Lafayette")+raw.count("Cpt. Lafayette")+raw.count("Captain Lafayette")+raw.count("Cpl. Lafayette")+raw.count("Corporal Lafayette")+raw.count("Pvt. Lafayette")+raw.count("Private Lafayette")+raw.count("Gen. Lafayette")+raw.count("General Lafayette")+raw.count("Admiral Lafayette"))
			LafayetteFNRE = re.compile("|".join(w + r"\s+" + "Lafayette" \
										for w in nltk.corpus.names.words()))
			LafayetteFNFindall = LafayetteFNRE.findall(raw)
			LafayetteFNTotal = len(LafayetteFNFindall)
			LafayetteNameTotal = (LafayetteFNTotal+TitleLafayetteTotal)
			ConTotalLafayette = LAtotal+LafayetteNameTotal
			if ConTotalLafayette > 0 :
				LAConPerLafayette = LAtotal/ConTotalLafayette
				NameConPerLafayette = LafayetteNameTotal/ConTotalLafayette
			else :
				LAConPerLafayette = 0
				NameConPerLafayette = 0
			RawLafayetteTotal = raw.count("Lafayette")
			RawLafayetteLessName = (RawLafayetteTotal-LafayetteNameTotal)
			PerLafayetteTotal = RawLafayetteLessName*LAConPerLafayette
			LAtotal = (LAtotal+PerLafayetteTotal)
			#Lowell
			TitleLowellTotal = (raw.count("Mr. Lowell")+raw.count("Ms. Lowell")+raw.count("Mrs. Lowell")+raw.count("Dr. Lowell")+raw.count("President Lowell")+raw.count("Pres. Lowell")+raw.count("Lt. Lowell")+raw.count("Lieutenant Lowell")+raw.count("Sgt. Lowell")+raw.count("Sergent Lowell")+raw.count("Cpt. Lowell")+raw.count("Captain Lowell")+raw.count("Cpl. Lowell")+raw.count("Corporal Lowell")+raw.count("Pvt. Lowell")+raw.count("Private Lowell")+raw.count("Gen. Lowell")+raw.count("General Lowell")+raw.count("Admiral Lowell"))
			LowellFNRE = re.compile("|".join(w + r"\s+" + "Lowell" \
										for w in nltk.corpus.names.words()))
			LowellFNFindall = LowellFNRE.findall(raw)
			LowellFNTotal = len(LowellFNFindall)
			LowellNameTotal = (LowellFNTotal+TitleLowellTotal)
			ConTotalLowell = MAtotal+LowellNameTotal
			if ConTotalLowell > 0 :
				MAConPerLowell = MAtotal/ConTotalLowell
				NameConPerLowell = LowellNameTotal/ConTotalLowell
			else :
				MAConPerLowell = 0
				NameConPerLowell = 0
			RawLowellTotal = raw.count("Lowell")
			RawLowellLessName = (RawLowellTotal-LowellNameTotal)
			PerLowellTotal = RawLowellLessName*MAConPerLowell
			MAtotal = (MAtotal+PerLowellTotal)
			#OFallon
			TitleOFallonTotal = (raw.count("Mr. O'Fallon")+raw.count("Ms. O'Fallon")+raw.count("Mrs. O'Fallon")+raw.count("Dr. O'Fallon")+raw.count("President O'Fallon")+raw.count("Pres. O'Fallon")+raw.count("Lt. O'Fallon")+raw.count("Lieutenant O'Fallon")+raw.count("Sgt. O'Fallon")+raw.count("Sergent O'Fallon")+raw.count("Cpt. O'Fallon")+raw.count("Captain O'Fallon")+raw.count("Cpl. O'Fallon")+raw.count("Corporal O'Fallon")+raw.count("Pvt. O'Fallon")+raw.count("Private O'Fallon")+raw.count("Gen. O'Fallon")+raw.count("General O'Fallon")+raw.count("Admiral O'Fallon"))
			OFallonFNRE = re.compile("|".join(w + r"\s+" + "O'Fallon" \
										for w in nltk.corpus.names.words()))
			OFallonFNFindall = OFallonFNRE.findall(raw)
			OFallonFNTotal = len(OFallonFNFindall)
			OFallonNameTotal = (OFallonFNTotal+TitleOFallonTotal)
			ConTotalOFallon = MOtotal+OFallonNameTotal
			if ConTotalOFallon > 0 :
				MOConPerOFallon = MOtotal/ConTotalOFallon
				NameConPerOFallon = OFallonNameTotal/ConTotalOFallon
			else :
				MOConPerOFallon = 0
				NameConPerOFallon = 0
			RawOFallonTotal = raw.count("O'Fallon")
			RawOFallonLessName = (RawOFallonTotal-OFallonNameTotal)
			PerOFallonTotal = RawOFallonLessName*MOConPerOFallon
			MOtotal = (MOtotal+PerOFallonTotal)
			#Kearney
			TitleKearneyTotal = (raw.count("Mr. Kearney")+raw.count("Ms. Kearney")+raw.count("Mrs. Kearney")+raw.count("Dr. Kearney")+raw.count("President Kearney")+raw.count("Pres. Kearney")+raw.count("Lt. Kearney")+raw.count("Lieutenant Kearney")+raw.count("Sgt. Kearney")+raw.count("Sergent Kearney")+raw.count("Cpt. Kearney")+raw.count("Captain Kearney")+raw.count("Cpl. Kearney")+raw.count("Corporal Kearney")+raw.count("Pvt. Kearney")+raw.count("Private Kearney")+raw.count("Gen. Kearney")+raw.count("General Kearney")+raw.count("Admiral Kearney"))
			KearneyFNRE = re.compile("|".join(w + r"\s+" + "Kearney" \
										for w in nltk.corpus.names.words()))
			KearneyFNFindall = KearneyFNRE.findall(raw)
			KearneyFNTotal = len(KearneyFNFindall)
			KearneyNameTotal = (KearneyFNTotal+TitleKearneyTotal)
			ConTotalKearney = NEtotal+KearneyNameTotal
			if ConTotalKearney > 0 :
				NEConPerKearney = NEtotal/ConTotalKearney
				NameConPerKearney = KearneyNameTotal/ConTotalKearney
			else :
				NEConPerKearney = 0
				NameConPerKearney = 0
			RawKearneyTotal = raw.count("Kearney")
			RawKearneyLessName = (RawKearneyTotal-KearneyNameTotal)
			PerKearneyTotal = RawKearneyLessName*NEConPerKearney
			NEtotal = (NEtotal+PerKearneyTotal)
			#Henderson
			TitleHendersonTotal = (raw.count("Mr. Henderson")+raw.count("Ms. Henderson")+raw.count("Mrs. Henderson")+raw.count("Dr. Henderson")+raw.count("President Henderson")+raw.count("Pres. Henderson")+raw.count("Lt. Henderson")+raw.count("Lieutenant Henderson")+raw.count("Sgt. Henderson")+raw.count("Sergent Henderson")+raw.count("Cpt. Henderson")+raw.count("Captain Henderson")+raw.count("Cpl. Henderson")+raw.count("Corporal Henderson")+raw.count("Pvt. Henderson")+raw.count("Private Henderson")+raw.count("Gen. Henderson")+raw.count("General Henderson")+raw.count("Admiral Henderson"))
			HendersonFNRE = re.compile("|".join(w + r"\s+" + "Henderson" \
										for w in nltk.corpus.names.words()))
			HendersonFNFindall = HendersonFNRE.findall(raw)
			HendersonFNTotal = len(HendersonFNFindall)
			HendersonNameTotal = (HendersonFNTotal+TitleHendersonTotal)
			ConTotalHenderson = NVtotal+HendersonNameTotal
			if ConTotalHenderson > 0 :
				NVConPerHenderson = NVtotal/ConTotalHenderson
				NameConPerHenderson = HendersonNameTotal/ConTotalHenderson
			else :
				NVConPerHenderson = 0
				NameConPerHenderson = 0
			RawHendersonTotal = raw.count("Henderson")
			RawHendersonLessName = (RawHendersonTotal-HendersonNameTotal)
			PerHendersonTotal = RawHendersonLessName*NVConPerHenderson
			NVtotal = (NVtotal+PerHendersonTotal)
			#Paterson
			TitlePatersonTotal = (raw.count("Mr. Paterson")+raw.count("Ms. Paterson")+raw.count("Mrs. Paterson")+raw.count("Dr. Paterson")+raw.count("President Paterson")+raw.count("Pres. Paterson")+raw.count("Lt. Paterson")+raw.count("Lieutenant Paterson")+raw.count("Sgt. Paterson")+raw.count("Sergent Paterson")+raw.count("Cpt. Paterson")+raw.count("Captain Paterson")+raw.count("Cpl. Paterson")+raw.count("Corporal Paterson")+raw.count("Pvt. Paterson")+raw.count("Private Paterson")+raw.count("Gen. Paterson")+raw.count("General Paterson")+raw.count("Admiral Paterson"))
			PatersonFNRE = re.compile("|".join(w + r"\s+" + "Paterson" \
										for w in nltk.corpus.names.words()))
			PatersonFNFindall = PatersonFNRE.findall(raw)
			PatersonFNTotal = len(PatersonFNFindall)
			PatersonNameTotal = (PatersonFNTotal+TitlePatersonTotal)
			ConTotalPaterson = NJtotal+PatersonNameTotal
			if ConTotalPaterson > 0 :
				NJConPerPaterson = NJtotal/ConTotalPaterson
				NameConPerPaterson = PatersonNameTotal/ConTotalPaterson
			else :
				NJConPerPaterson = 0
				NameConPerPaterson = 0
			RawPatersonTotal = raw.count("Paterson")
			RawPatersonLessName = (RawPatersonTotal-PatersonNameTotal)
			PerPatersonTotal = RawPatersonLessName*NJConPerPaterson
			NJtotal = (NJtotal+PerPatersonTotal)
			#Raleigh
			TitleRaleighTotal = (raw.count("Mr. Raleigh")+raw.count("Ms. Raleigh")+raw.count("Mrs. Raleigh")+raw.count("Dr. Raleigh")+raw.count("President Raleigh")+raw.count("Pres. Raleigh")+raw.count("Lt. Raleigh")+raw.count("Lieutenant Raleigh")+raw.count("Sgt. Raleigh")+raw.count("Sergent Raleigh")+raw.count("Cpt. Raleigh")+raw.count("Captain Raleigh")+raw.count("Cpl. Raleigh")+raw.count("Corporal Raleigh")+raw.count("Pvt. Raleigh")+raw.count("Private Raleigh")+raw.count("Gen. Raleigh")+raw.count("General Raleigh")+raw.count("Admiral Raleigh"))
			RaleighFNRE = re.compile("|".join(w + r"\s+" + "Raleigh" \
										for w in nltk.corpus.names.words()))
			RaleighFNFindall = RaleighFNRE.findall(raw)
			RaleighFNTotal = len(RaleighFNFindall)
			RaleighNameTotal = (RaleighFNTotal+TitleRaleighTotal)
			ConTotalRaleigh = NCtotal+RaleighNameTotal
			if ConTotalRaleigh > 0 :
				NCConPerRaleigh = NCtotal/ConTotalRaleigh
				NameConPerRaleigh = RaleighNameTotal/ConTotalRaleigh
			else :
				NCConPerRaleigh = 0
				NameConPerRaleigh = 0
			RawRaleighTotal = raw.count("Raleigh")
			RawRaleighLessName = (RawRaleighTotal-RaleighNameTotal)
			PerRaleighTotal = RawRaleighLessName*NCConPerRaleigh
			NCtotal = (NCtotal+PerRaleighTotal)
			#Durham
			TitleDurhamTotal = (raw.count("Mr. Durham")+raw.count("Ms. Durham")+raw.count("Mrs. Durham")+raw.count("Dr. Durham")+raw.count("President Durham")+raw.count("Pres. Durham")+raw.count("Lt. Durham")+raw.count("Lieutenant Durham")+raw.count("Sgt. Durham")+raw.count("Sergent Durham")+raw.count("Cpt. Durham")+raw.count("Captain Durham")+raw.count("Cpl. Durham")+raw.count("Corporal Durham")+raw.count("Pvt. Durham")+raw.count("Private Durham")+raw.count("Gen. Durham")+raw.count("General Durham")+raw.count("Admiral Durham"))
			DurhamFNRE = re.compile("|".join(w + r"\s+" + "Durham" \
										for w in nltk.corpus.names.words()))
			DurhamFNFindall = DurhamFNRE.findall(raw)
			DurhamFNTotal = len(DurhamFNFindall)
			DurhamNameTotal = (DurhamFNTotal+TitleDurhamTotal)
			ConTotalDurham = NCtotal+DurhamNameTotal
			if ConTotalDurham > 0 :
				NCConPerDurham = NCtotal/ConTotalDurham
				NameConPerDurham = DurhamNameTotal/ConTotalDurham
			else :
				NCConPerDurham = 0
				NameConPerDurham = 0
			RawDurhamTotal = raw.count("Durham")
			RawDurhamLessName = (RawDurhamTotal-DurhamNameTotal)
			PerDurhamTotal = RawDurhamLessName*NCConPerDurham
			NCtotal = (NCtotal+PerDurhamTotal)
			#Moore
			TitleMooreTotal = (raw.count("Mr. Moore")+raw.count("Ms. Moore")+raw.count("Mrs. Moore")+raw.count("Dr. Moore")+raw.count("President Moore")+raw.count("Pres. Moore")+raw.count("Lt. Moore")+raw.count("Lieutenant Moore")+raw.count("Sgt. Moore")+raw.count("Sergent Moore")+raw.count("Cpt. Moore")+raw.count("Captain Moore")+raw.count("Cpl. Moore")+raw.count("Corporal Moore")+raw.count("Pvt. Moore")+raw.count("Private Moore")+raw.count("Gen. Moore")+raw.count("General Moore")+raw.count("Admiral Moore"))
			MooreFNRE = re.compile("|".join(w + r"\s+" + "Moore" \
										for w in nltk.corpus.names.words()))
			MooreFNFindall = MooreFNRE.findall(raw)
			MooreFNTotal = len(MooreFNFindall)
			MooreNameTotal = (MooreFNTotal+TitleMooreTotal)
			ConTotalMoore = OKtotal+MooreNameTotal
			if ConTotalMoore > 0 :
				OKConPerMoore = OKtotal/ConTotalMoore
				NameConPerMoore = MooreNameTotal/ConTotalMoore
			else :
				OKConPerMoore = 0
				NameConPerMoore = 0
			RawMooreTotal = raw.count("Moore")
			RawMooreLessName = (RawMooreTotal-MooreNameTotal)
			PerMooreTotal = RawMooreLessName*OKConPerMoore
			OKtotal = (OKtotal+PerMooreTotal)
			#Cranston
			TitleCranstonTotal = (raw.count("Mr. Cranston")+raw.count("Ms. Cranston")+raw.count("Mrs. Cranston")+raw.count("Dr. Cranston")+raw.count("President Cranston")+raw.count("Pres. Cranston")+raw.count("Lt. Cranston")+raw.count("Lieutenant Cranston")+raw.count("Sgt. Cranston")+raw.count("Sergent Cranston")+raw.count("Cpt. Cranston")+raw.count("Captain Cranston")+raw.count("Cpl. Cranston")+raw.count("Corporal Cranston")+raw.count("Pvt. Cranston")+raw.count("Private Cranston")+raw.count("Gen. Cranston")+raw.count("General Cranston")+raw.count("Admiral Cranston"))
			CranstonFNRE = re.compile("|".join(w + r"\s+" + "Cranston" \
										for w in nltk.corpus.names.words()))
			CranstonFNFindall = CranstonFNRE.findall(raw)
			CranstonFNTotal = len(CranstonFNFindall)
			CranstonNameTotal = (CranstonFNTotal+TitleCranstonTotal)
			ConTotalCranston = RItotal+CranstonNameTotal
			if ConTotalCranston > 0 :
				RIConPerCranston = RItotal/ConTotalCranston
				NameConPerCranston = CranstonNameTotal/ConTotalCranston
			else :
				RIConPerCranston = 0
				NameConPerCranston = 0
			RawCranstonTotal = raw.count("Cranston")
			RawCranstonLessName = (RawCranstonTotal-CranstonNameTotal)
			PerCranstonTotal = RawCranstonLessName*RIConPerCranston
			RItotal = (RItotal+PerCranstonTotal)
			#Houston
			TitleHoustonTotal = (raw.count("Mr. Houston")+raw.count("Ms. Houston")+raw.count("Mrs. Houston")+raw.count("Dr. Houston")+raw.count("President Houston")+raw.count("Pres. Houston")+raw.count("Lt. Houston")+raw.count("Lieutenant Houston")+raw.count("Sgt. Houston")+raw.count("Sergent Houston")+raw.count("Cpt. Houston")+raw.count("Captain Houston")+raw.count("Cpl. Houston")+raw.count("Corporal Houston")+raw.count("Pvt. Houston")+raw.count("Private Houston")+raw.count("Gen. Houston")+raw.count("General Houston")+raw.count("Admiral Houston"))
			HoustonFNRE = re.compile("|".join(w + r"\s+" + "Houston" \
										for w in nltk.corpus.names.words()))
			HoustonFNFindall = HoustonFNRE.findall(raw)
			HoustonFNTotal = len(HoustonFNFindall)
			HoustonNameTotal = (HoustonFNTotal+TitleHoustonTotal)
			ConTotalHouston = TXtotal+HoustonNameTotal
			if ConTotalHouston > 0 :
				TXConPerHouston = TXtotal/ConTotalHouston
				NameConPerHouston = HoustonNameTotal/ConTotalHouston
			else :
				TXConPerHouston = 0
				NameConPerHouston = 0
			RawHoustonTotal = raw.count("Houston")
			RawHoustonLessName = (RawHoustonTotal-HoustonNameTotal)
			PerHoustonTotal = RawHoustonLessName*TXConPerHouston
			TXtotal = (TXtotal+PerHoustonTotal)
			#Sheridan
			TitleSheridanTotal = (raw.count("Mr. Sheridan")+raw.count("Ms. Sheridan")+raw.count("Mrs. Sheridan")+raw.count("Dr. Sheridan")+raw.count("President Sheridan")+raw.count("Pres. Sheridan")+raw.count("Lt. Sheridan")+raw.count("Lieutenant Sheridan")+raw.count("Sgt. Sheridan")+raw.count("Sergent Sheridan")+raw.count("Cpt. Sheridan")+raw.count("Captain Sheridan")+raw.count("Cpl. Sheridan")+raw.count("Corporal Sheridan")+raw.count("Pvt. Sheridan")+raw.count("Private Sheridan")+raw.count("Gen. Sheridan")+raw.count("General Sheridan")+raw.count("Admiral Sheridan"))
			SheridanFNRE = re.compile("|".join(w + r"\s+" + "Sheridan" \
										for w in nltk.corpus.names.words()))
			SheridanFNFindall = SheridanFNRE.findall(raw)
			SheridanFNTotal = len(SheridanFNFindall)
			SheridanNameTotal = (SheridanFNTotal+TitleSheridanTotal)
			ConTotalSheridan = WYtotal+SheridanNameTotal
			if ConTotalSheridan > 0 :
				WYConPerSheridan = WYtotal/ConTotalSheridan
				NameConPerSheridan = SheridanNameTotal/ConTotalSheridan
			else :
				WYConPerSheridan = 0
				NameConPerSheridan = 0
			RawSheridanTotal = raw.count("Sheridan")
			RawSheridanLessName = (RawSheridanTotal-SheridanNameTotal)
			PerSheridanTotal = RawSheridanLessName*WYConPerSheridan
			WYtotal = (WYtotal+PerSheridanTotal)
			#Garland
			TitleGarlandTotal = (raw.count("Mr. Garland")+raw.count("Ms. Garland")+raw.count("Mrs. Garland")+raw.count("Dr. Garland")+raw.count("President Garland")+raw.count("Pres. Garland")+raw.count("Lt. Garland")+raw.count("Lieutenant Garland")+raw.count("Sgt. Garland")+raw.count("Sergent Garland")+raw.count("Cpt. Garland")+raw.count("Captain Garland")+raw.count("Cpl. Garland")+raw.count("Corporal Garland")+raw.count("Pvt. Garland")+raw.count("Private Garland")+raw.count("Gen. Garland")+raw.count("General Garland")+raw.count("Admiral Garland"))
			GarlandFNRE = re.compile("|".join(w + r"\s+" + "Garland" \
										for w in nltk.corpus.names.words()))
			GarlandFNFindall = GarlandFNRE.findall(raw)
			GarlandFNTotal = len(GarlandFNFindall)
			GarlandNameTotal = (GarlandFNTotal+TitleGarlandTotal)
			ConTotalGarland = TXtotal+GarlandNameTotal
			if ConTotalGarland > 0 :
				TXConPerGarland = TXtotal/ConTotalGarland
				NameConPerGarland = GarlandNameTotal/ConTotalGarland
			else :
				TXConPerGarland = 0
				NameConPerGarland = 0
			RawGarlandTotal = raw.count("Garland")
			RawGarlandLessName = (RawGarlandTotal-GarlandNameTotal)
			PerGarlandTotal = RawGarlandLessName*TXConPerGarland
			TXtotal = (TXtotal+PerGarlandTotal)
			#Stockton
			TitleStocktonTotal = (raw.count("Mr. Stockton")+raw.count("Ms. Stockton")+raw.count("Mrs. Stockton")+raw.count("Dr. Stockton")+raw.count("President Stockton")+raw.count("Pres. Stockton")+raw.count("Lt. Stockton")+raw.count("Lieutenant Stockton")+raw.count("Sgt. Stockton")+raw.count("Sergent Stockton")+raw.count("Cpt. Stockton")+raw.count("Captain Stockton")+raw.count("Cpl. Stockton")+raw.count("Corporal Stockton")+raw.count("Pvt. Stockton")+raw.count("Private Stockton")+raw.count("Gen. Stockton")+raw.count("General Stockton")+raw.count("Admiral Stockton"))
			StocktonFNRE = re.compile("|".join(w + r"\s+" + "Stockton" \
										for w in nltk.corpus.names.words()))
			StocktonFNFindall = StocktonFNRE.findall(raw)
			StocktonFNTotal = len(StocktonFNFindall)
			StocktonNameTotal = (StocktonFNTotal+TitleStocktonTotal)
			ConTotalStockton = CAtotal+StocktonNameTotal
			if ConTotalStockton > 0 :
				CAConPerStockton = CAtotal/ConTotalStockton
				NameConPerStockton = StocktonNameTotal/ConTotalStockton
			else :
				CAConPerStockton = 0
				NameConPerStockton = 0
			RawStocktonTotal = raw.count("Stockton")
			RawStocktonLessName = (RawStocktonTotal-StocktonNameTotal)
			PerStocktonTotal = RawStocktonLessName*CAConPerStockton
			CAtotal = (CAtotal+PerStocktonTotal)
#Washington disambiguation
			TitleWashingtontotal = (raw.count("Mr. Washington")+raw.count("Ms. Washington")+raw.count("Mrs. Washington")+raw.count("Dr. Washington")+raw.count("President Washington")+raw.count("Pres. Washington")+raw.count("Lt. Washington")+raw.count("Lieutenant Washington")+raw.count("Sgt. Washington")+raw.count("Sergent Washington")+raw.count("Cpt. Washington")+raw.count("Captain Washington")+raw.count("Cpl. Washington")+raw.count("Corporal Washington")+raw.count("Pvt. Washington")+raw.count("Private Washington")+raw.count("Gen. Washington")+raw.count("General Washington")+raw.count("Admiral Washington"))
			WashingtonFNRE = re.compile("|".join(w + r"\s+" + "Washington" \
										for w in nltk.corpus.names.words()))
			WashingtonFNFindall = WashingtonFNRE.findall(raw)
			WashingtonFNtotal = len(WashingtonFNFindall)
			WashingtonNametotal = (WashingtonFNtotal+TitleWashingtontotal)
			ContotalWashington = DCtotal+WAtotal+WashingtonNametotal
			if ContotalWashington > 0 :
				DCConPerWashington = DCtotal/ContotalWashington
				WAConPerWashington = WAtotal/ContotalWashington
				NameConPerWashington = WashingtonNametotal/ContotalWashington
			else :
				DCConPerWashington = 0
				WAConPerWashington = 0
				NameConPerWashington = 0
			if DCtotal > 10* WAtotal :
				NOWAContotalWashington = DCtotal+WashingtonNametotal
				NOWANameConPerWashington = WashingtonNametotal/NOWAContotalWashington
				NOWADCConPerWashington = DCtotal/NOWAContotalWashington
				RawWashingtontotal = raw.count("Washington")
				RawWashingtonLessName = (RawWashingtontotal-WashingtonNametotal)
				DCtotal = (DCtotal+(RawWashingtonLessName*NOWADCConPerWashington))
			else:
				RawWashingtontotal = raw.count("Washington")
				RawWashingtonLessName = (RawWashingtontotal-WashingtonNametotal)
				DCtotal = (DCtotal+(RawWashingtonLessName*DCConPerWashington))
				WAtotal = (WAtotal+(RawWashingtonLessName*WAConPerWashington))
#Collecting region counts
			Northeasttotal = (MEtotal+VTtotal+NHtotal+MAtotal+CTtotal+RItotal+NYtotal+PAtotal+NJtotal)
			Southtotal = (WVtotal+MDtotal+DEtotal+DCtotal+KYtotal+VAtotal+OKtotal+ARtotal+TNtotal+NCtotal+TXtotal+LAtotal+MStotal+ALtotal+GAtotal+SCtotal+FLtotal)
			Midwesttotal = (NDtotal+MNtotal+WItotal+MItotal+SDtotal+IAtotal+ILtotal+INtotal+OHtotal+NEtotal+KStotal+MOtotal)
			Westtotal = (AKtotal+HItotal+WAtotal+IDtotal+MTtotal+ORtotal+WYtotal+CAtotal+NVtotal+UTtotal+COtotal+AZtotal+NMtotal)
#Iowa percentage of text count
			IAfocus = 100*(IAtotal/totallen)
#Calculating state percentages
			TotalStatetotal = (DCtotal+CTtotal+WAtotal+MEtotal+ALtotal+AKtotal+AZtotal+ARtotal+CAtotal+COtotal+DEtotal+FLtotal+GAtotal+HItotal+IDtotal+ILtotal+INtotal+IAtotal+KStotal+KYtotal+LAtotal+MDtotal+MAtotal+MItotal+MNtotal+MStotal+MOtotal+MTtotal+NEtotal+NVtotal+NHtotal+NJtotal+NMtotal+NYtotal+NCtotal+NDtotal+OHtotal+OKtotal+ORtotal+PAtotal+RItotal+SCtotal+SDtotal+TNtotal+TXtotal+UTtotal+VTtotal+VAtotal+WVtotal+WItotal+WYtotal)
			if TotalStatetotal > 0 :
				ALperof = 100*(ALtotal/TotalStatetotal)
				AKperof = 100*(AKtotal/TotalStatetotal)
				AZperof = 100*(AZtotal/TotalStatetotal)
				ARperof = 100*(ARtotal/TotalStatetotal)
				CAperof = 100*(CAtotal/TotalStatetotal)
				COperof = 100*(COtotal/TotalStatetotal)
				CTperof = 100*(CTtotal/TotalStatetotal)
				DCperof = 100*(DCtotal/TotalStatetotal)
				DEperof = 100*(DEtotal/TotalStatetotal)
				FLperof = 100*(FLtotal/TotalStatetotal)
				GAperof = 100*(GAtotal/TotalStatetotal)
				HIperof = 100*(HItotal/TotalStatetotal)
				IDperof = 100*(IDtotal/TotalStatetotal)
				ILperof = 100*(ILtotal/TotalStatetotal)
				INperof = 100*(INtotal/TotalStatetotal)
				IAperof = 100*(IAtotal/TotalStatetotal)
				KSperof = 100*(KStotal/TotalStatetotal)
				KYperof = 100*(KYtotal/TotalStatetotal)
				LAperof = 100*(LAtotal/TotalStatetotal)
				MEperof = 100*(MEtotal/TotalStatetotal)
				MDperof = 100*(MDtotal/TotalStatetotal)
				MAperof = 100*(MAtotal/TotalStatetotal)
				MIperof = 100*(MItotal/TotalStatetotal)
				MNperof = 100*(MNtotal/TotalStatetotal)
				MSperof = 100*(MStotal/TotalStatetotal)
				MOperof = 100*(MOtotal/TotalStatetotal)
				MTperof = 100*(MTtotal/TotalStatetotal)
				NEperof = 100*(NEtotal/TotalStatetotal)
				NVperof = 100*(NVtotal/TotalStatetotal)
				NHperof = 100*(NHtotal/TotalStatetotal)
				NJperof = 100*(NJtotal/TotalStatetotal)
				NMperof = 100*(NMtotal/TotalStatetotal)
				NYperof = 100*(NYtotal/TotalStatetotal)
				NCperof = 100*(NCtotal/TotalStatetotal)
				NDperof = 100*(NDtotal/TotalStatetotal)
				OHperof = 100*(OHtotal/TotalStatetotal)
				OKperof = 100*(OKtotal/TotalStatetotal)
				ORperof = 100*(ORtotal/TotalStatetotal)
				PAperof = 100*(PAtotal/TotalStatetotal)
				RIperof = 100*(RItotal/TotalStatetotal)
				SCperof = 100*(SCtotal/TotalStatetotal)
				SDperof = 100*(SDtotal/TotalStatetotal)
				TNperof = 100*(TNtotal/TotalStatetotal)
				TXperof = 100*(TXtotal/TotalStatetotal)
				UTperof = 100*(UTtotal/TotalStatetotal)
				VTperof = 100*(VTtotal/TotalStatetotal)
				VAperof = 100*(VAtotal/TotalStatetotal)
				WAperof = 100*(WAtotal/TotalStatetotal)
				WVperof = 100*(WVtotal/TotalStatetotal)
				WIperof = 100*(WItotal/TotalStatetotal)
				WYperof = 100*(WYtotal/TotalStatetotal)
				Northeastperof = 100*(Northeasttotal/TotalStatetotal)
				Southperof = 100*(Southtotal/TotalStatetotal)
				Midwestperof = 100*(Midwesttotal/TotalStatetotal)
				Westperof = 100*(Westtotal/TotalStatetotal)
			else :
				ALperof = 0
				AKperof = 0
				AZperof = 0
				ARperof = 0
				CAperof = 0
				COperof = 0
				CTperof = 0
				DCperof = 0
				DEperof = 0
				FLperof = 0
				GAperof = 0
				HIperof = 0
				IDperof = 0
				ILperof = 0
				INperof = 0
				IAperof = 0
				KSperof = 0
				KYperof = 0
				LAperof = 0
				MEperof = 0
				MDperof = 0
				MAperof = 0
				MIperof = 0
				MNperof = 0
				MSperof = 0
				MOperof = 0
				MTperof = 0
				NEperof = 0
				NVperof = 0
				NHperof = 0
				NJperof = 0
				NMperof = 0
				NYperof = 0
				NCperof = 0
				NDperof = 0
				OHperof = 0
				OKperof = 0
				ORperof = 0
				PAperof = 0
				RIperof = 0
				SCperof = 0
				SDperof = 0
				TNperof = 0
				TXperof = 0
				UTperof = 0
				VTperof = 0
				VAperof = 0
				WAperof = 0
				WVperof = 0
				WIperof = 0
				WYperof = 0
				Northeastperof = 0
				Southperof = 0
				Midwestperof = 0
				Westperof = 0
#Creating output data
			totaltuples = [('File Name',	
			'Total Mention Count',
			'AL Mention Score','AL % of Total Mentions',
			'AK Mention Score','AK % of Total Mentions',
			'AZ Mention Score','AZ % of Total Mentions',
			'AR Mention Score','AR % of Total Mentions',
			'CA Mention Score','CA % of Total Mentions',
			'CO Mention Score','CO % of Total Mentions',
			'CT Mention Score','CT % of Total Mentions',
			'DC Mention Score','DC % of Total Mentions',
			'DE Mention Score','DE % of Total Mentions',
			'FL Mention Score','FL % of Total Mentions',
			'GA Mention Score','GA % of Total Mentions',
			'HI Mention Score','HI % of Total Mentions',
			'ID Mention Score','ID % of Total Mentions',
			'IL Mention Score','IL % of Total Mentions',
			'IN Mention Score','IN % of Total Mentions',
			'IA Mention Score','IA % of Total Mentions',
			'KS Mention Score','KS % of Total Mentions',
			'KY Mention Score','KY % of Total Mentions',
			'LA Mention Score','LA % of Total Mentions',
			'ME Mention Score','ME % of Total Mentions',
			'MD Mention Score','MD % of Total Mentions',
			'MA Mention Score','MA % of Total Mentions',
			'MI Mention Score','MI % of Total Mentions',
			'MN Mention Score','MN % of Total Mentions',
			'MS Mention Score','MS % of Total Mentions',
			'MO Mention Score','MO % of Total Mentions',
			'MT Mention Score','MT % of Total Mentions',
			'NE Mention Score','NE % of Total Mentions',
			'NV Mention Score','NV % of Total Mentions',
			'NH Mention Score','NH % of Total Mentions',
			'NJ Mention Score','NJ % of Total Mentions',
			'NM Mention Score','NM % of Total Mentions',
			'NY Mention Score','NY % of Total Mentions',
			'NC Mention Score','NC % of Total Mentions',
			'ND Mention Score','ND % of Total Mentions',
			'OH Mention Score','OH % of Total Mentions',
			'OK Mention Score','OK % of Total Mentions',
			'OR Mention Score','OR % of Total Mentions',
			'PA Mention Score','PA % of Total Mentions',
			'RI Mention Score','RI % of Total Mentions',
			'SC Mention Score','SC % of Total Mentions',
			'SD Mention Score','SD % of Total Mentions',
			'TN Mention Score','TN % of Total Mentions',
			'TX Mention Score','TX % of Total Mentions',
			'UT Mention Score','UT % of Total Mentions',
			'VT Mention Score','VT % of Total Mentions',
			'VA Mention Score','VA % of Total Mentions',
			'WA Mention Score','WA % of Total Mentions',
			'WV Mention Score','WV % of Total Mentions',
			'WI Mention Score','WI % of Total Mentions',
			'WY Mention Score','WY % of Total Mentions',
			'Total % of corpus Iowa Mentions',
			'Northeast Mention Score', 'Northeast % of Total Mentions',
			'South Mention Score', 'South % of Total Mentions',
			'Midwest Mention Score', 'Midwest % of Total Mentions',
			'West Mention Score', 'West % of Total Mentions'),
			(nameonly,
			TotalStatetotal,
			ALtotal,ALperof,
			AKtotal,AKperof,
			AZtotal,AZperof,
			ARtotal,ARperof,
			CAtotal,CAperof,
			COtotal,COperof,
			CTtotal,CTperof,
			DCtotal,DCperof,
			DEtotal,DEperof,
			FLtotal,FLperof,
			GAtotal,GAperof,
			HItotal,HIperof,
			IDtotal,IDperof,
			ILtotal,ILperof,
			INtotal,INperof,
			IAtotal,IAperof,
			KStotal,KSperof,
			KYtotal,KYperof,
			LAtotal,LAperof,
			MEtotal,MEperof,
			MDtotal,MDperof,
			MAtotal,MAperof,
			MItotal,MIperof,
			MNtotal,MNperof,
			MStotal,MSperof,
			MOtotal,MOperof,
			MTtotal,MTperof,
			NEtotal,NEperof,
			NVtotal,NVperof,
			NHtotal,NHperof,
			NJtotal,NJperof,
			NMtotal,NMperof,
			NYtotal,NYperof,
			NCtotal,NCperof,
			NDtotal,NDperof,
			OHtotal,OHperof,
			OKtotal,OKperof,
			ORtotal,ORperof,
			PAtotal,PAperof,
			RItotal,RIperof,
			SCtotal,SCperof,
			SDtotal,SDperof,
			TNtotal,TNperof,
			TXtotal,TXperof,
			UTtotal,UTperof,
			VTtotal,VTperof,
			VAtotal,VAperof,
			WAtotal,WAperof,
			WVtotal,WVperof,
			WItotal,WIperof,
			WYtotal,WYperof,
			IAfocus,
			Northeasttotal, Northeastperof,
			Southtotal, Southperof,
			Midwesttotal, Midwestperof,
			Westtotal, Westperof),]
#Outputting file to .csv
			outputfullpath = (selectend+"/"+nameonly+".csv")
			with open(outputfullpath, 'w') as totalresult:
				writer = csv.writer (totalresult, dialect='excel')
				writer.writerows(totaltuples)
			print("Metrics for file ", basename,"done. Check containing folder.")
print("Run complete.")